package com.company.project.controller.ldjsc.concern;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.ldjsc.concern.HbAppMenuUserEntity;
import com.company.project.service.ldjsc.concern.HbAppMenuService;
import com.company.project.service.ldjsc.concern.HbAppMenuUserService;
import com.company.project.service.ldjsc.menu.ZyMenuService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author wenbin
 * @email *****@mail.com
 * @date 2025-07-03 09:33:29
 */
@RestController
@RequestMapping("/sxzy/muneUser")
@Api(tags = "领导驾驶仓-关注")
public class HbAppMenuUserController {

    @Autowired
    private HbAppMenuService hbAppMenuService;
    @Autowired
    private HbAppMenuUserService hbAppMenuUserService;
    @Autowired
    private ZyMenuService zyMenuService;

    /**
     * 跳转到页面
     */
    @GetMapping("/index/hbAppMenuUser")
    public String hbAppMenuUser() {
        return "hbappmenuuser/list";
    }


    @ApiOperation(value = "查询分页数据")
    @PostMapping("hbAppMenuUser/listByPage")
    @SaCheckPermission("hbAppMenuUser:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody HbAppMenuUserEntity hbAppMenuUser) {
        LambdaQueryWrapper<HbAppMenuUserEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.eq(hbAppMenuUser.getId() != null, HbAppMenuUserEntity::getId, hbAppMenuUser.getId());
        queryWrapper.orderByDesc(HbAppMenuUserEntity::getId);
        IPage<HbAppMenuUserEntity> iPage = hbAppMenuUserService.page(hbAppMenuUser.getQueryPage(), queryWrapper);
        return DataResult.success(iPage);
    }

    /**
     * 新增（关注）
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    @LogAnnotation(title = "领导驾驶仓", action = "关注")
//    @SaCheckPermission("hbAppMenuUser:save")
    @ResponseBody
    public DataResult add(@RequestBody HbAppMenuUserEntity hbAppMenuUser) throws Exception {
        if (StringUtils.isBlank(hbAppMenuUser.getAppCode()) ||
                StringUtils.isBlank(hbAppMenuUser.getUserId()) ||
                StringUtils.isBlank(hbAppMenuUser.getAppMenuId())) {
            return DataResult.fail("参数错误！");
        }

        //判断当前用户是否已经关注该菜单
        List<HbAppMenuUserEntity> list = hbAppMenuUserService.list(new LambdaQueryWrapper<HbAppMenuUserEntity>()
                .eq(HbAppMenuUserEntity::getAppMenuId, hbAppMenuUser.getAppMenuId())
                .eq(HbAppMenuUserEntity::getUserId, hbAppMenuUser.getUserId())
                .eq(HbAppMenuUserEntity::getAppCode, hbAppMenuUser.getAppCode()));
        if (list.size() > 0){
            return DataResult.fail("已关注！");
        }
        hbAppMenuUserService.save(hbAppMenuUser);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("hbAppMenuUser/delete")
    @SaCheckPermission("hbAppMenuUser:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        hbAppMenuUserService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("hbAppMenuUser/update")
    @SaCheckPermission("hbAppMenuUser:update")
    @ResponseBody
    public DataResult update(@RequestBody HbAppMenuUserEntity hbAppMenuUser) {
        hbAppMenuUserService.updateById(hbAppMenuUser);
        return DataResult.success();
    }

    /**
     * 取消关注
     */
    @ApiOperation(value = "取消关注")
    @PostMapping("/cancel")
    @ResponseBody
    public DataResult cancel(@RequestBody HbAppMenuUserEntity hbAppMenuUser) throws Exception {
        if (StringUtils.isBlank(hbAppMenuUser.getAppCode()) ||
                StringUtils.isBlank(hbAppMenuUser.getUserId()) ||
                StringUtils.isBlank(hbAppMenuUser.getAppMenuId())) {
            return DataResult.fail("参数错误！");
        }
        hbAppMenuUserService.remove(new LambdaQueryWrapper<HbAppMenuUserEntity>()
                .eq(HbAppMenuUserEntity::getAppMenuId, hbAppMenuUser.getAppMenuId())
                .eq(HbAppMenuUserEntity::getUserId, hbAppMenuUser.getUserId())
                .eq(HbAppMenuUserEntity::getAppCode, hbAppMenuUser.getAppCode()));
        return DataResult.success();
    }

    /**
     * 列表（我的关注）
     */
    @GetMapping("/list/user")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "列表", notes = "传入menu")
    @ResponseBody
    public DataResult<List<HbAppMenuUserEntity>> listUer(String userId, String mendId) {

        //查询当前用户关注的菜单
        List<HbAppMenuUserEntity> menuUserList = hbAppMenuUserService.list(new LambdaQueryWrapper<HbAppMenuUserEntity>()
                .eq(HbAppMenuUserEntity::getUserId, userId)
                .eq(StringUtils.isNotBlank(mendId), HbAppMenuUserEntity::getAppMenuId, mendId)
                .orderByDesc(HbAppMenuUserEntity::getCreateDate));
        return DataResult.success(menuUserList);
    }
}
